package com.jilin.library.mapper;

import com.jilin.library.entity.BorrowRecord;
import org.apache.ibatis.annotations.*;
import java.util.List;

@Mapper
public interface BorrowRecordMapper {
    @Select("SELECT * FROM borrow_record")
    @Results({
        @Result(property = "reader", column = "reader_id", 
            one = @One(select = "com.jilin.library.mapper.ReaderMapper.findById")),
        @Result(property = "book", column = "book_id", 
            one = @One(select = "com.jilin.library.mapper.BookMapper.findById"))
    })
    List<BorrowRecord> findAll();
    
    @Select("SELECT * FROM borrow_record WHERE id = #{id}")
    @Results({
        @Result(property = "reader", column = "reader_id", 
            one = @One(select = "com.jilin.library.mapper.ReaderMapper.findById")),
        @Result(property = "book", column = "book_id", 
            one = @One(select = "com.jilin.library.mapper.BookMapper.findById"))
    })
    BorrowRecord findById(Integer id);
    
    @Insert("INSERT INTO borrow_record(reader_id, book_id, status) " +
            "VALUES(#{readerId}, #{bookId}, 0)")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(BorrowRecord record);
    
    @Update("UPDATE borrow_record SET return_date = NOW(), status = 1 " +
            "WHERE id = #{id}")
    int returnBook(Integer id);
} 